package com.practice.springboot.springbootMybatis.controller;

import com.practice.springboot.springbootMybatis.model.Book;
import com.practice.springboot.springbootMybatis.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author sai
 * @create 2020-08-23 3:39
 */
@RestController
public class BookController {
    @Autowired
    BookService bookService ;

    @RequestMapping("/insert")
    public int addBook( ){
        Book book = new Book() ;
        book.setName("不灭经");
        book.setAuthor("荒天帝");
        return bookService.insertBook(book) ;
    }

    @RequestMapping("/deleteBook")
    public int deleteBook( @RequestParam("id") Long id){
        return bookService.deleteBook(id) ;
    }

    @RequestMapping("/updateBook")
    public int updateBook( @RequestParam("id") Long id,
                           @RequestParam("name") String name,
                           @RequestParam("author") String author){
        Book book = new Book() ;
        book.setId(id);
        book.setName(name);
        book.setAuthor(author);
        return bookService.updateBook(book) ;
    }



    @RequestMapping("/queryBook")
    public Book queryBookById( @RequestParam("id") Long id){
        return bookService.queryBookById(id) ;
    }

    @RequestMapping("/getAllBooks")
    public List<Book> getAllBooks( ){
        return  bookService.getAllBooks() ;
    }

}
